 import Vue from 'vue';
// 引入element UI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App';
//px转rem 
import '../node_modules/lib-flexible/flexible'  
// 引入路由
import router from './router';
// 引入状态管理
import store from './vuex/store';
// 引入icon
import './assets/icon/iconfont.css'

import '../static/common.css'; 
 

// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;

import axios from 'axios';
Vue.prototype.$axios = axios; 
Vue.config.productionTip = false; 

// 使用element UI
Vue.use(ElementUI);
// 过滤器
import * as custom from './utils/util'
const secretKey = '123456321456';
const CryptoJS = require('crypto-js');


Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key])
})
let info =window.sessionStorage.getItem("uussffoo"),type = ''; 
if(info){
  const bytes = CryptoJS.AES.decrypt(info, secretKey);
  info = bytes.toString(CryptoJS.enc.Utf8)
}

if (info !== null && info !== undefined && info !== '') { 
  info = JSON.parse(info); 
  type = info.userType;
}
// 路由拦截器
router.beforeEach((to, from, next) => { 
  if (to.matched.length != 0) {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限 
      if (custom.getStoreS("ssttkktt")) { // 通过vuex state获取当前的user是否存在
        next();
      } else { 
        if( type == "00") {
          next({
            path: '/loginAdmin',
            query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
          })
        } else  if( type == "01") {
          next({
            path: '/login',
            query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
          })
        } else {
          next()
        }
      }
    } else {
      
      if (custom.getStoreS("ssttkktt")) { // 判断是否登录
        if (to.path != "/" && to.path != "/login" && to.path != "/loginAdmin") {
          next();
        } else {
          if(type == "00") {
            next({
              path: '/walletAdmin'
            })
          } else  {
            next({
              path: '/wallet'
            })
          }
          
        }
      } else {
        next();
      }
    }
  } else {
    if (type == "00") {
      next({
        path: '/loginAdmin',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else  if( type == "01") {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      next()
    }
  }
})

let roles = []
if(info&&info.params&&info.params.roles){
  roles= info.params.roles
}
  
 
Vue.directive('hasPermission',{
  //会在指令作用的元素插入到页面完成后触发
  inserted(el,binding){
  // el-指令作用元素dom对象 
  if((info.userType=='03' || info.info=='02')){
    const points= roles
      //获取权限点---? 链接符-当前位置无值,后面不会执行
  if(!points.includes(binding.value)){
      // 移除或则禁用
      el.remove()
      // el.disabled=true
      }
  }
  }
 
 }) 

 Vue.directive('copy',{
    inserted(el,binding){
    // el-指令作用元素dom对象 
    if((info.userType=='03' || info.info=='02')){
      const points= roles
        //获取权限点---? 链接符-当前位置无值,后面不会执行
    if(!points.includes(binding.value)){
        // 移除或则禁用
        el.remove()
        // el.disabled=true
        }
    }
    }
 }) 
 // 定义全局方法
Vue.prototype.$hasPermission = function (userinfo,type) {    
  roles= userinfo.params.roles || info.params.roles;
  const user11 = userinfo || info
 
  if((user11.userType=='03' || user11.userType=='02')){
    if(type.some(item => roles.includes(item))){
      return true
    }else{
      return false
    }   
  }else{
    return true
  }
  // console.log(userinfo.userType,'userinfo.userType')
  // console.log(type,'type')
  // console.log(roles,'roles') 
  // console.log(type.some(item => roles.includes(item)),'22222')
 
} 
 


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store, //使用store vuex状态管理
  components: { App },
  template: '<App/>',
  data: {
    // 空的实例放到根组件下，所有的子组件都能调用
    Bus: new Vue()
  }

})